package week9;

import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class RMSOperate
{
  private String fileName = "phoneIndex2";

  public boolean addIndex(Index paramIndex)
  {
    byte[] arrayOfByte;
    try
    {
      arrayOfByte = paramIndex.serialize();
      RecordStore localRecordStore = RecordStore.openRecordStore(this.fileName, true);
      if (localRecordStore.getNumRecords() > 0)
      {
        RecordEnumeration localRecordEnumeration = localRecordStore.enumerateRecords(new RMSOperate.IndexFilter1(fileName), null, false);
        if (localRecordEnumeration.hasNextElement())
        {
          localRecordStore.closeRecordStore();
          return false;
        }
        localRecordStore.addRecord(arrayOfByte, 0, arrayOfByte.length);
        localRecordStore.closeRecordStore();
        return true;
      }
      localRecordStore.addRecord(arrayOfByte, 0, arrayOfByte.length);
      localRecordStore.closeRecordStore();
      return true;
    }
    catch (RecordStoreFullException localRecordStoreFullException)
    {
    }
    catch (RecordStoreNotFoundException localRecordStoreNotFoundException)
    {
    }
    catch (IOException localIOException)
    {
    }
    catch (RecordStoreException localRecordStoreException)
    {
    }
    return false;
  }

  public boolean deleteIndex(String paramString)
  {
    RecordStore localRecordStore;
    try
    {
      localRecordStore = RecordStore.openRecordStore(this.fileName, true);
      if (localRecordStore.getNumRecords() > 0)
      {
        RecordEnumeration localRecordEnumeration = localRecordStore.enumerateRecords(new RMSOperate.IndexFilter1(paramString), null, false);
        if (localRecordEnumeration.hasNextElement())
        {
          localRecordStore.deleteRecord(localRecordEnumeration.nextRecordId());
          localRecordStore.closeRecordStore();
          return true;
        }
        localRecordStore.closeRecordStore();
        return false;
      }
      localRecordStore.closeRecordStore();
      return true;
    }
    catch (RecordStoreFullException localRecordStoreFullException)
    {
    }
    catch (RecordStoreNotFoundException localRecordStoreNotFoundException)
    {
    }
    catch (RecordStoreException localRecordStoreException)
    {
    }
    return false;
  }

  public boolean modiIndex(int paramInt, Index paramIndex)
  {
    byte[] arrayOfByte;
    try
    {
      arrayOfByte = paramIndex.serialize();
      try
      {
        RecordStore localRecordStore = RecordStore.openRecordStore(this.fileName, true);
        localRecordStore.setRecord(paramInt, arrayOfByte, 0, arrayOfByte.length);
        localRecordStore.closeRecordStore();
        return true;
      }
      catch (RecordStoreNotOpenException localRecordStoreNotOpenException)
      {
      }
      catch (InvalidRecordIDException localInvalidRecordIDException)
      {
      }
      catch (RecordStoreFullException localRecordStoreFullException)
      {
      }
      catch (RecordStoreException localRecordStoreException)
      {
      }
      return false;
    }
    catch (IOException localIOException)
    {
    }
    return false;
  }

  public Index[] serachIndex(String paramString, boolean paramBoolean)
  {
    Index[] arrayOfIndex;
    RMSOperate.IndexFilter1 localIndexFilter = null;
    if ((null != paramString) && (0 != paramString.length()))
      localIndexFilter = new RMSOperate.IndexFilter1(paramString);
    try
    {
      RecordStore localRecordStore = RecordStore.openRecordStore(this.fileName, true);
      if (localRecordStore.getNumRecords() > 0)
      {
        RecordEnumeration localRecordEnumeration = localRecordStore.enumerateRecords(localIndexFilter, null, false);
        int i = localRecordEnumeration.numRecords();
        if (0 == i)
        {
          arrayOfIndex = new Index[0];
        }
        else
        {
          arrayOfIndex = new Index[i];
          try
          {
            int j = localRecordEnumeration.nextRecordId();
            FirstID.getInstance().setFirstID(j);
            arrayOfIndex[0] = Index.deserialize(localRecordStore.getRecord(j));
            for (int k = 1; k < i; ++k)
              arrayOfIndex[k] = Index.deserialize(localRecordStore.getRecord(localRecordEnumeration.nextRecordId()));
          }
          catch (InvalidRecordIDException localInvalidRecordIDException)
          {
            arrayOfIndex = new Index[0];
          }
          catch (IOException localIOException)
          {
            arrayOfIndex = new Index[0];
          }
          catch (RecordStoreException localRecordStoreException2)
          {
            arrayOfIndex = new Index[0];
          }
        }
      }
      else
      {
        arrayOfIndex = new Index[0];
      }
      localRecordStore.closeRecordStore();
    }
    catch (RecordStoreNotOpenException localRecordStoreNotOpenException)
    {
      arrayOfIndex = new Index[0];
    }
    catch (RecordStoreFullException localRecordStoreFullException)
    {
      arrayOfIndex = new Index[0];
    }
    catch (RecordStoreNotFoundException localRecordStoreNotFoundException)
    {
      arrayOfIndex = new Index[0];
    }
    catch (RecordStoreException localRecordStoreException1)
    {
      arrayOfIndex = new Index[0];
    }
    return arrayOfIndex;
  }

  private class IndexFilter1
  implements RecordFilter
  {
    private String key;
    private boolean type;
    private final RMSOperate this$01;
	private RMSOperate paramRMSOperate;
	private byte[] paramArrayOfByte;

    public IndexFilter1(String paramString)
    {
      this.this$01 = paramRMSOperate;
      this.type = true;
      this.key = paramString;
    }

    public IndexFilter1( String paramString, boolean paramBoolean)
    {
      this.this$01 = paramRMSOperate;
      this.type = true;
      this.key = paramString;
      this.type = paramBoolean;
    }

    public boolean matches()
    {
      try
      {
        return Index.matches(paramArrayOfByte, this.key, this.type);
      }
      catch (IOException localIOException)
      {
        localIOException.printStackTrace();
      }
      return false;
    }

	public boolean matches(byte[] arg0) {
		// TODO Auto-generated method stub
		return false;
	}
  }
}